package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.dto.PageDto;
import com.qf.pojo.DtsAd;
import com.qf.pojo.DtsArticle;
import com.qf.service.DtsAdService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/25 10:54
 */

@RestController
@RequestMapping("/admin/ad")
@Slf4j
@CrossOrigin
public class AdController {
    @Autowired
    private DtsAdService dtsAdService;
    //Request URL: http://localhost:8080/admin/ad/list?page=1&limit=20&name=dd&content=ss&sort=add_time&order=desc

    @RequiresPermissionsDesc(menu = {"推广管理","广告管理"},button ="查找")
    @RequiresPermissions("admin:ad:list")
    @GetMapping("/list")
    public  Object adList(PageDto pageDto){

        if(pageDto==null){
            return ResponseUtil.badArgumentValue();
        }
     Page<DtsAd> dtsAdPage= dtsAdService.selectPage(pageDto);
        long total = dtsAdPage.getTotal();
        List<DtsAd> records = dtsAdPage.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","广告管理"},button ="编辑")
    @RequiresPermissions("admin:ad:update")
    @PostMapping("/update")
    public  Object updateAd(@RequestBody DtsAd dtsAd){
        if(dtsAd==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsAd.setUpdateTime(new Date());
        dtsAdService.update(dtsAd);
        return ResponseUtil.ok(dtsAd);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","广告管理"},button ="添加")
    @RequiresPermissions("admin:ad:create")
    @PostMapping("/create")
    public  Object createAd(@RequestBody DtsAd dtsAd){
        if(dtsAd==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsAd.setUpdateTime(new Date());
        dtsAd.setAddTime(new Date());
        dtsAd.setDeleted(false);
        dtsAdService.insert(dtsAd);
        return ResponseUtil.ok(dtsAd);
    }
    @RequiresPermissionsDesc(menu = {"推广管理","广告管理"},button ="删除")
    @RequiresPermissions("admin:ad:delete")
    @PostMapping("/delete")
    public  Object deleteAd(@RequestBody DtsAd dtsAd){
        if(dtsAd==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsAd.setUpdateTime(new Date());
        dtsAd.setDeleted(true);
        dtsAdService.delete(dtsAd);
        return ResponseUtil.ok();}
}
